package com.lanou.tipsy.fragment.recommend;

import android.content.Context;
import android.net.Uri;
import android.nfc.Tag;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lanou.tipsy.R;
import com.lanou.tipsy.base.BaseFragment;
import com.lanou.tipsy.entity.recommend.guess.singer.RightListEntity;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by FengJun on 15/10/24.
 * 推荐->猜你喜欢->歌手
 */
public class TodaySingerFragment extends BaseFragment implements View.OnClickListener {
    public static final String TAG = "TodaySingerFragment";
    private ArrayList<RightListEntity> singerListEntities;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private Context context;
    private Toolbar toolbar;
    private SimpleDraweeView draweeView;
    private CollapsingToolbarLayout toolbarLayout;
    private RightListEntity entity;
    private String singerId;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fr_recommend_guess_singer, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();
        singerListEntities = (ArrayList<RightListEntity>) bundle.get("todaySinger");
        int today = (int) bundle.get("today");
        entity = singerListEntities.get(today);
        singerId = entity.getTing_uid();
        initView(getView());
        String name = singerListEntities.get(today).getName();
        if (name != null && name != "") {
            toolbarLayout.setTitle(name);
        }
        String url = singerListEntities.get(today).getAvatar_big();
        if (url != null && url != "") {
            Uri uri = Uri.parse(url);
            draweeView.setImageURI(uri);
        }
    }

    private void initView(View view) {
        tabLayout = (TabLayout) view.findViewById(R.id.tablayout_guess_singer);
        viewPager = (ViewPager) view.findViewById(R.id.viewpager_guess_singer);
        draweeView = (SimpleDraweeView) view.findViewById(R.id.draw_guess_singer);
        toolbarLayout = (CollapsingToolbarLayout) view.findViewById(R.id.ctl_guess_singer);
        toolbar = (Toolbar) view.findViewById(R.id.tl_today);
        toolbar.setNavigationIcon(R.mipmap.btn_web_back_normal);
        toolbar.setNavigationOnClickListener(this);
        MyAdapter adapter = new MyAdapter(getChildFragmentManager());
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.color_grey_deep));
        tabLayout.setTabTextColors(getResources().getColor(R.color.color_grey_deep),
                getResources().getColor(R.color.color_black_main));
    }

    @Override
    public void onClick(View v) {
        getActivity().getSupportFragmentManager().popBackStack();
    }

    //viewpager适配器内部类
    public class MyAdapter extends FragmentPagerAdapter {
        private String[] titles = {"歌曲", "专辑"};
        private Map<String, SoftReference<ArrayList<Fragment>>> softReferenceMap = new HashMap<>();
        private ArrayList<Fragment> fragments;

        //添加软引用对象
        private void addFragments(String path) {
            Log.i("test", TAG + "->添加软应用对象");
            //创建强引用对象
            ArrayList<Fragment> fragments = new ArrayList<>();
            fragments.add(new TodaySingerSongFragment(singerId));
            fragments.add(new TodaySingerAlbumFragment(singerId));
            //软引用对象
            SoftReference<ArrayList<Fragment>> softReference = new SoftReference<>(fragments);
            //将该对象添加到HashMap中使其缓存
            softReferenceMap.put(path, softReference);
        }

        //获得软引用对象
        private ArrayList<Fragment> getFragmentsByPath(String path) {
            Log.i("test", TAG + "->获取软引用对象");
            //从缓存中获取软引用集合对象
            SoftReference<ArrayList<Fragment>> softReference = softReferenceMap.get(path);
            if (softReference == null) {
                return null;
            }
            fragments = softReference.get();
            return fragments;
        }

        public MyAdapter(FragmentManager fm) {
            super(fm);
            addFragments(TAG);
            getFragmentsByPath(TAG);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.size() > 0 && fragments != null ? fragments.get(position) : null;
        }

        @Override
        public int getCount() {
            return fragments.size() > 0 && fragments != null ? fragments.size() : null;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }
}
